<?php
use yii\helpers\Url;

$web = Url::base();
$url = \app\librarys\Tools::urlBase();
?>

<script type="application/javascript">

    var isRefresh = 0;
    var insSelect2;  //父机构下拉
    var ob;

    $(function ($) {
        loading.hide();
        ob = "<?=$ob ?>";
        if (ob == '1') {
            $("#orderByTotal").prop('class', "glyphicon glyphicon-chevron-down");
        } else {
            $("#orderByTotal").prop('class', "glyphicon glyphicon-chevron-up");
        }
    });


    //搜索
    function search() {
        var status = $("#select1").val();
        /*var filter;
         var filterP;*/
        var f = $('#total').val()._trim();
        var institution = $('#searchInstitution').val()._trim();
        var href = window.location.href;
        var index = href.indexOf("?");
        if (index > 0) {
            href = href.substr(0, index);
        }
        if (f != '') {
            href = setUrlParam(href, 'total', f);
        }
        if (institution != '') {
            href = setUrlParam(href, 'institution', institution);
        }
        if (ob != '') {
            href = setUrlParam(href, 'ob', ob);
        }

        window.location.href = href;
        loading.show();
    }

    //排序
    function orderByTotal() {

        if ($("#orderByTotal").prop('class') == "glyphicon glyphicon-chevron-up") {
            $("#orderByTotal").prop('class', "glyphicon glyphicon-chevron-down");
            ob = '1';
        } else {
            $("#orderByTotal").prop('class', "glyphicon glyphicon-chevron-up");
            ob = '0';
        }
        search();
    }

    //刷新页面
    function refresh() {
        loading.show();
        if (isRefresh != 0) {
            location.reload();
        } else {
            var href = window.location.href;
            var index = href.indexOf("?");
            if (index > 0) {
                href = href.substr(0, index);
            }
            window.location.href = href;
        }
        isRefresh = 0;
    }


</script>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="pull-right">
                <div class="form-group">
                    <?= \app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                </div>
                <div class="form-group">
                    <input type="number" class="form-control" id="total" placeholder="可用额小于等于" value="<?= $total ?>"
                           onkeypress="if(event.keyCode==13) {search();return false;}">

                    <div class="btn-group">
                        <button type="button" class="btn btn-primary" onclick="search()"><i class="fa fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed"
                   id="institutionTable">
                <thead>
                <tr class="nowrap">
                    <th class="col-lg-1">机构编码</th>
                    <th class="col-lg-4">机构名称</th>
                    <th>账户余额</th>
                    <th>授信额</th>
                    <th>可用额 <a href="#" rel="drevil"><span class="glyphicon glyphicon-chevron-up"
                                                           onclick="orderByTotal()"
                                                           id="orderByTotal"></span></a>
                    </th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                for ($i = 0; $i < count($record); $i++) {
                    ?>
                    <tr>
                        <td><?= $record[$i]['code'] ?></td>
                        <td><?= $record[$i]['name'] ?></td>
                        <td><?= $record[$i]['balance'] ?></td>
                        <td><?= $record[$i]['creditQuota'] ?></td>
                        <td><?= $record[$i]['total'] ?></td>
                        <td>
                            <a class="btn btn btn-link btn-xs" type="button" data-toggle="modal" href="#creditMode"
                               data-institution-code="<?= $record[$i]['code'] ?>"
                               data-institution-name="<?= $record[$i]['name'] ?>">授信申请
                            </a>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>


<div aria-hidden="true" role="dialog" tabindex="-1" id="creditMode" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">新增授信</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">机构:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="institutionName" disabled="true">
                            <input class="hidden_id" id="inputInstitution" type="hidden" value="0"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">申请人:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputApplyUserName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">授信额度:</label>
                        <div class="col-lg-9">
                            <div class="input-group m-bot15">
                                <span class="input-group-addon">￥</span>
                                <input type="text" class="form-control" id="inputCreditQuota">
                                <span class="input-group-addon ">.00</span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label col-lg-3">备注:</label>
                        <div class="col-lg-9">
                            <textarea rows="7" class="form-control" id="inputRemark"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnCreateCreditOk">确定</button>
            </div>
        </div>
    </div>
</div>


<script type="application/javascript">

    (function () {
        $('#creditMode').on('show.bs.modal', function (e) {
            $('#inputInstitution').val(e.relatedTarget.attributes['data-institution-code'].value);
            $('#institutionName').val(e.relatedTarget.attributes['data-institution-name'].value);
        });
        $('#btnCreateCreditOk').click(function () {
            var data = {};
            data.institution_code = $('#inputInstitution').val();
            data.apply_user_name = $('#inputApplyUserName').val();
            data.credit_quota = $('#inputCreditQuota').val();
            data.remark = $('#inputRemark').val();
            if (data.credit_quota==null || data.credit_quota=='') {
                alert('请输入授信额度!');
                return;
            }else if (data.credit_quota<=0) {
                alert('授信额度必须大于0!');
                return;
            }
            requestJson('<?= $url ?>/finance/add-credit-handler', data, true, function (responseData) {
                $('#addModal').modal('hide');
                if (responseData.status == 0) {
                    window.location.reload();
                } else {
                    alert(responseData.message);
                }
            });
        });

    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#creditMode').modal({
            backdrop: 'static', show: false
        });
    });
</script>

